package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.Materials;
import com.ruoyi.base.domain.vo.MaterialsVo;
import com.ruoyi.base.service.IMaterialsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料管理Controller
 *
 * @author YwaiX
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/base/materials")
public class MaterialsController extends BaseController {
    @Autowired
    private IMaterialsService materialsService;

    /**
     * 查询物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:materials:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialsVo materials) {
        startPage();
        List<MaterialsVo> list = materialsService.selectMaterialsList(materials);
        return getDataTable(list);
    }

    /**
     * 导出物料管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:materials:export')")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Materials materials) {
        List<Materials> list = materialsService.selectMaterials(materials);
        ExcelUtil<Materials> util = new ExcelUtil<Materials>(Materials.class);
        util.exportExcel(response, list, "物料管理数据");
    }

    /**
     * 获取物料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:materials:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialsService.selectMaterialsById(id));
    }

    /**
     * 新增物料管理
     */
    @PreAuthorize("@ss.hasPermi('base:materials:add')")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialsVo materials) {
        materials.setCreateBy(getLoginUser().getUserId()+"");
        return toAjax(materialsService.insertMaterials(materials));
    }

    /**
     * 修改物料管理
     */
    @PreAuthorize("@ss.hasPermi('base:materials:edit')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialsVo materials) {
        materials.setUpdateBy(getLoginUser().getUserId()+"");
        return toAjax(materialsService.updateMaterials(materials));
    }

    /**
     * 删除物料管理
     */
    @PreAuthorize("@ss.hasPermi('base:materials:remove')")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(materialsService.deleteMaterialsByIds(ids));
    }

    /**
     * 撤销删除物料管理
     */
    @PreAuthorize("@ss.hasPermi('base:materials:revokeDelete')")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PutMapping("/revokeDeleteDelMaterials/{ids}")
    public AjaxResult revokeDeleteDelMaterials(@PathVariable Long ids) {
        return toAjax(materialsService.revokeDeleteDelMaterials(ids));
    }
}
